//
//  GYMineProductImageView.m
//  GYDemo
//
//  Created by mac on 2018/6/30.
//  Copyright © 2018年 mac. All rights reserved.
//

#import "GYMineProductImageView.h"
#import "Head.h"

@interface GYMineProductImageView()
@property (nonatomic,strong) UIImageView *chooseBTN;
@property (nonatomic,strong) UIScrollView *contentSV;
@end

@implementation GYMineProductImageView
#pragma mark - getter and setter
-(NSMutableArray *)imageS
{
    if (!_imageS) {
        _imageS = [NSMutableArray new];
    }
    return _imageS;
}
-(UIImageView *)chooseBTN
{
    if (!_chooseBTN) {
        _chooseBTN = [UIImageView new];
        _chooseBTN.userInteractionEnabled = YES;
        _chooseBTN.image = [UIImage imageNamed:@"shop_addimage"];
        @WeakSelf;
        [_chooseBTN addTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
            [selfp choosePhoto];
        }];
    }
    _chooseBTN.x = self.contentX;
    _chooseBTN.y = self.contentY;
    _chooseBTN.height = self.height - self.contentY * 2;
    _chooseBTN.width = _chooseBTN.height;
    return _chooseBTN;
}
-(UIScrollView *)contentSV
{
    if (!_contentSV) {
        _contentSV = [UIScrollView new];
        _contentSV.pagingEnabled = YES;
    }
    _contentSV.x = _chooseBTN.right + self.contentSpaceX;
    _contentSV.height = self.height;
    _contentSV.width = self.width - _contentSV.x - self.contentSpaceX * 2;
    return _contentSV;
}
-(void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    self.chooseBTN;
    self.contentSV;
//    NSLog(@"%@",self.chooseBTN);
//    NSLog(@"%@",self.contentSV);
    [self layoutSubviews];
}
#pragma mark - method
-(void)choosePhoto
{
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:_maxImageCount - self.imageS.count delegate:nil];
    imagePickerVc.allowTakePicture = NO;
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    [imagePickerVc setNavLeftBarButtonSettingBlock:^(UIButton *leftButton) {
        //  [self dismissViewControllerAnimated:YES completion:nil];
    }];
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    @WeakSelf;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [FTIndicator showProgressWithMessage:@"正在上传图片"];
        [[GYAliyunOSS sharedInstance] uploadImages:photos isAsync:YES complete:^(NSArray<NSString *> *names, NSString *state) {
            [(UIViewController *)self.delegate performSelectorOnMainThread:@selector(dissmissFTIndicatorProgress) withObject:nil waitUntilDone:YES];
            for (NSString *pics in names) {
                [selfp.imageS addObject:pics];
                [[(GCBaseVC *)selfp.delegate baseTable] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
                [selfp.delegate chooseImages:selfp.imageS];
            }
        }];
    }];
    [(UIViewController *)self.delegate presentViewController:imagePickerVc animated:YES completion:nil];
}
#pragma mark - life
-(instancetype)init
{
    if (self = [super init]) {
        self.width = SCREEN_WIDTH;
        self.height = WFCGFloatY(127);
        _maxImageCount = 10;
        _contentX = WFCGFloatX(13);
        _contentY = WFCGFloatY(10);
        _contentSpaceX = WFCGFloatX(10);
        _contentSpaceY = 0;
        [self addSubview:self.chooseBTN];
        [self addSubview:self.contentSV];
    }
    return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)layoutSubviews
{
    [super layoutSubviews];
    [self.contentSV removeAllSubviews];
    _contentSV.contentSize = CGSizeMake((_contentSV.height + _contentSpaceX) * self.imageS.count, 0);
    for (int i = 0; i < self.imageS.count; i++) {
        UIImageView *locationIV = [[UIImageView alloc] init];
        locationIV.width = _chooseBTN.height;
        locationIV.height = _chooseBTN.height;
        locationIV.x = (_chooseBTN.height + _contentSpaceX) * i;
        locationIV.y = _chooseBTN.y;
        NSString *pic = self.imageS[i];
        NSString *pics = [NSString stringWithFormat:@"%@%@",pic, kProductImgThumbnailSuffix];
        [locationIV sd_setImageWithURL:[NSURL URLWithString:pics] placeholderImage:DefaultImage];
        
        UIButton *deleIV = [[UIButton alloc] init];
        UIImage *image = [UIImage imageNamed:@"shopimage_del"];
        [deleIV setBackgroundImage:image forState:UIControlStateNormal];
        deleIV.width = image.size.width;
        deleIV.height = image.size.height;
        [deleIV setEnlargeEdge:10];
        deleIV.centerX = locationIV.right;
        deleIV.centerY = locationIV.y;
        deleIV.tag = 100 + i;
        @WeakSelf;
        [deleIV addTapGestureRecognizerWithDelegate:self Block:^(NSInteger tag) {
            [selfp.imageS removeObjectAtIndex:i];
            [[(GCBaseVC *)selfp.delegate baseTable] performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
            [selfp.delegate chooseImages:selfp.imageS];
        }];
        [_contentSV addSubview:locationIV];
        [_contentSV addSubview:deleIV];
    }
}
@end
